package com.sysofstudent.web.stu;

import com.sysofstudent.dao.CourseRepository;
import com.sysofstudent.dao.ScoreRepository;
import com.sysofstudent.po.Course;
import com.sysofstudent.po.Score;
import com.sysofstudent.po.Student;
import com.sysofstudent.service.CourseService;
import com.sysofstudent.service.ScoreService;
import com.sysofstudent.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ScoreController {

    @Resource
    CourseService courseService;
    @Autowired
    ScoreService scoreService;
    @Autowired
    StudentService studentService;
    @Autowired
    CourseRepository courseRepository;
    @Autowired
    ScoreRepository scoreRepository;


    @RequestMapping("admin/score")
    public String score(Model model)
    {
        List<Course> courses=courseService.findAllCourse();
        model.addAttribute("courses",courses);
        return "admin/score";
    }

    @RequestMapping("admin/toAddCourse")
    public String toAddCourse(Model model)
    {
        Course course=new Course();
        model.addAttribute("course",course);
        return"admin/AddCourse";
    }
    @RequestMapping("admin/addCourse")
    public String addCourse(Course course)
    {
        courseRepository.save(course);
        return ("redirect:/admin/score");
    }


//                <td><a th:href="@{'/admin/showScore'+@{course.id}}">查看</a></td>

    @RequestMapping("/admin/showScore{course_id}")
    public String showScore(Model model,@PathVariable long course_id)
    {
//        List<Score> scores=scoreService.findByCourse_id(course_id);
        List<Score> scores=scoreRepository.findScoreByCourse_id(course_id);
//        List<Score> scores=scoreRepository.findAll();
        Course course1=courseRepository.findById(course_id);
        model.addAttribute("scores",scores);
        model.addAttribute("course1",course1);
        return "admin/ShowScore";
    }

    @RequestMapping("/admin/toChange{id}")
    public String toChange(Model model,@PathVariable long id)
    {
        Score score=scoreRepository.findScoreById(id);
        model.addAttribute("score",score);
        return "admin/toChange";
    }

    @RequestMapping("/admin/Change")
    public String Change(Score score)
    {
//        Course course=courseRepository.findById(3);
//        score.setCourse(course);
        scoreRepository.save(score);
        return ("redirect:/admin/score");
    }
    @RequestMapping("/admin/toAddScore{id}")
    public String toAdd(Model model,@PathVariable long id)
    {
        Score score = new Score();
        Course course=courseRepository.findById(id);
        score.setCourse(course);
        model.addAttribute("score",score);
        return "/admin/toAddScore";
    }
    @RequestMapping("/admin/AddScore")
    public String Add(Score score)
    {
        scoreRepository.save(score);
        return ("redirect:/admin/score");
    }
//    th:href="'/admin/deleteScore'+${score.id}" >删除</a></td>

    @RequestMapping("/admin/deleteScore{id}")
    public String deleteScore(@PathVariable long id)
    {
        scoreRepository.deleteById(id);
        return ("redirect:/admin/score");
    }
//    <td><a th:href="@{'/admin/deleteCourse'+${course.id}}">删除</a></td>

    @RequestMapping("/admin/deleteCourse{id}")
    public String deleteCourse(@PathVariable long id)
    {
//        List<Score> scores=scoreService.findByCourse_id(id);
        //        courseRepository.deleteById(id);
        return ("redirect:/admin/score");

    }
    @RequestMapping("/student/score{number}")
    public String stu_score(Model model,@PathVariable String number)
    {
        Student student=studentService.findStudent(number);
        List<Score> scores=scoreService.findByNumber(number);
        model.addAttribute("scores",scores);
        model.addAttribute("student",student);
        return "student/score";
    }

//    @RequestMapping("/student/score{number}")
//    public String score(Model model,@PathVariable String number)
//    {
//        List<Score> scores=scoreService.getScoreListBynumber(number);
//        Student student =studentService.findStudent(number);
//        model.addAttribute("student",student);
//        model.addAttribute("scores",scores);
//        return "/student/score";
//    }
//
//    @RequestMapping("/admin/score")
//    public String adscore(Model model)
//    {
//        List<Score> scores = scoreService.getCourseList();
//        model.addAttribute("scores",scores);
//        return "/admin/score";
//    }
//
//    @RequestMapping("/admin/showScore{course}")
//    public String showScore(Model model,@PathVariable String course)
//    {
//        String kecheng=course;
//        List<Score> scores =scoreService.getScoreListBycourse(course);
//        model.addAttribute("kecheng",kecheng);
//        model.addAttribute("scores",scores);
//        return "/admin/ShowScore";
//    }
//
//    @RequestMapping("/admin/AddCourse")
//    public String AddCourse(Model model)
//    {
//        Score score=new Score();
//        model.addAttribute("score",score);
//        return "admin/AddCourse";
//    }
//
//    @RequestMapping("/admin/addCourse")
//    public String addcourse(Score score)
//    {
//        scoreService.save(score);
//        return "redirect:/admin/score";
//    }
//
//    @RequestMapping("/admin/toAddScore{kecheng}")
//    public String toAddScore(Model model,@PathVariable String kecheng)
//    {
//        Score score=new Score();
//        score.setCourse(kecheng);
//        model.addAttribute("score",score);
//        return "admin/toAddScore";
//    }
//    @RequestMapping("/admin/AddScore")
//    public String AddScore(Model model,Score score)
//    {
//        scoreService.save(score);
//        String course=score.getCourse();
//        String kecheng=course;
//        List<Score> scores =scoreService.getScoreListBycourse(course);
//        model.addAttribute("kecheng",kecheng);
//        model.addAttribute("scores",scores);
//        return "/admin/ShowScore";
//    }
//
//    @RequestMapping("/admin/toDelete{id}")
//    public  String toDelete(@PathVariable Long id)
//    {
//        scoreService.delete(id);
//        return "redirect:/admin/score";
//    }
//    @RequestMapping("/admin/delete{course}")
//    public String delete(@PathVariable String course)
//    {
//        List<Score> scores =scoreService.getScoreListBycourse2(course);
//        scoreService.delete(scores);
//        return "redirect:/admin/score";
//    }
//    @RequestMapping("/admin/toChange{id}")
//    public String toChange(Model model,@PathVariable Long id)
//    {
//        Score score=scoreService.findScoreByid(id);
//        model.addAttribute("score",score);
//        return "/admin/toChange";
//    }
//    @RequestMapping("/admin/Change")
//    public String change (Score score)
//    {
//        scoreService.edit(score);
//        return "redirect:/admin/score";
//    }
//
//
//    @RequestMapping("/get")
//    public String get(Model model)
//    {
//        List<Score> scores =scoreService.findall();
//        model.addAttribute("scores",scores);
//        return "/get";
//    }
}
